//
//  SingleMovieDetailViewViewController.m
//  WisdomZhuHai
//
//  Created by Ryan on 14-12-11.
//  Copyright (c) 2014年 wyd. All rights reserved.
//

#import "SingleMovieDetailViewController.h"
#import "ChooseCinemaViewController.h"
#import "HotRateView.h"
#import "UIImageView+LBBlurredImage.h"

static const CGFloat TITLECELL_HEIGHT = 180;
static const CGFloat DIRECTORACTORCELL = 120;
static const CGFloat INTROCELL = 200;

@interface SingleMovieDetailViewController ()

@property (nonatomic,strong)NSDictionary* movieDetail;

@end

@implementation SingleMovieDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.tableView.backgroundColor = [Common colorWithRGB:0xffffff];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //    [self setEnableRefresh:YES];
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 0.f)];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 0.f)];
    self.title = @"影片详情";
    [self getSingleMovieDetail];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Protocol request
- (void)getSingleMovieDetail
{
    [[ProtocolManager shareInstance]getSingleGoodDetail:self.goodId sendType:@"电影详情" target:self selector:@selector(getSingleMovieDetailCallback:)];
}

#pragma mark - Callback 
- (void)getSingleMovieDetailCallback:(NSDictionary*)dic
{
    NSLog(@"getSingleMovieDetailCallback %@",dic);
    if (dic == nil) {
        return;
    }
    self.movieDetail = [Common valueForKey:P_MOVIE dic:dic];
    [self.tableView reloadData];
}

#pragma mark - cell
//描绘title cell
- (void)loadTitleCell:(UITableViewCell*)cell
{
    if (self.movieDetail == nil) {
        return;
    }
    NSDictionary *dataCell = self.movieDetail;
    UIImageView *movieBg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, TITLECELL_HEIGHT)];
    NSString *imgUrl = [Common valueForKey:P_COVER dic:dataCell];
    [movieBg setImageWithURL:[Common getFullDownloadPathByRelativePath:imgUrl]];
    movieBg.image = [self imageFromImage:movieBg.image inRect:CGRectMake(0, 50, 320, 50)];
    
    [movieBg setImageToBlur:movieBg.image
                        blurRadius:kLBBlurredImageDefaultBlurRadius
                   completionBlock:^(NSError *error){
                       NSLog(@"The blurred image has been setted");
                   }];
    
    [cell.contentView addSubview:movieBg];
    UIImageView *movieImage = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 110, 140)];
    [movieImage setImageWithURL:[Common getFullDownloadPathByRelativePath:imgUrl]];
    [cell.contentView addSubview:movieImage];
        
    NSString *movieName = [Common valueForKey:P_NAME dic:dataCell];
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 25, cell.bounds.size.width-140-10, 20)];
    nameLabel.textColor = [UIColor whiteColor];
    nameLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:18.0];
    nameLabel.text = movieName;
    [cell.contentView addSubview:nameLabel];
    
    NSString *movieKind = [Common valueForKey:P_KIND dic:dataCell];
    int starY = nameLabel.frame.origin.y + 25;
    if (movieKind != nil) {
        NSArray *movieKindArray = [movieKind componentsSeparatedByString:@","];
        int kindImgX = 140;
        for( NSString *kind in movieKindArray)
        {
            UIImageView *kindImg = nil;
            if([kind isEqualToString:@"3D"]) {
                kindImg = [[UIImageView alloc] initWithFrame:CGRectMake(kindImgX, nameLabel.frame.origin.y + 25, 20, 20)];
                kindImg.image = [UIImage imageNamed:@"movie/movie_3d.png"];
                starY = 75;
            }
            else if ([kind isEqualToString:@"IMAX3D"]){
                kindImg = [[UIImageView alloc] initWithFrame:CGRectMake(kindImgX, nameLabel.frame.origin.y + 25, 40, 20)];
                kindImg.image = [UIImage imageNamed:@"movie/movie_imax3d.png"];
                starY = 75;
            }
            else if ([kind isEqualToString:@"IMAX2D"]){
                kindImg = [[UIImageView alloc] initWithFrame:CGRectMake(kindImgX, nameLabel.frame.origin.y + 25, 40, 20)];
                kindImg.image = [UIImage imageNamed:@"movie/movie_imax2d.png"];
                starY = 75;
            }
            else {
                break;
            }
            [cell.contentView addSubview:kindImg];
            kindImgX = kindImgX + 25;
        }
    }
    
    CGFloat star = [[Common valueForKey:P_RATING dic:dataCell] floatValue]/2;
    HotRateView *hotView = [[HotRateView alloc] initWithHotRate:star];
    CGRect frame = hotView.frame;
    frame.origin.x = 140;
    frame.origin.y = starY;
    hotView.frame = frame;
    [cell.contentView addSubview:hotView];
    
    NSString *movieType = [Common valueForKey:P_TYPE dic:dataCell];
    UILabel *typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, hotView.frame.origin.y+20, cell.bounds.size.width-140-10, 20)];
    typeLabel.textColor = [UIColor whiteColor];
    typeLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:15.0];
    typeLabel.text = [NSString stringWithFormat:@"类型：%@",movieType];
    [cell.contentView addSubview:typeLabel];
    
    NSString *movieLength = [Common valueForKey:P_LENGTH dic:dataCell];
    UILabel *lengthLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, typeLabel.frame.origin.y+20, cell.bounds.size.width-140-10, 20)];
    lengthLabel.textColor = [UIColor whiteColor];
    lengthLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:15.0];
    lengthLabel.text = [NSString stringWithFormat:@"片长：%@",movieLength];
    [cell.contentView addSubview:lengthLabel];
    
    NSString *moviePublishTime = [Common valueForKey:P_PUBLISH_TIME dic:dataCell];
    UILabel *publishTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, lengthLabel.frame.origin.y+20, cell.bounds.size.width-140-10, 20)];
    publishTimeLabel.textColor = [UIColor whiteColor];
    publishTimeLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:15.0];
    NSArray *publishTimeArray = [moviePublishTime componentsSeparatedByString:@"日"];
    publishTimeLabel.text = [NSString stringWithFormat:@"上映：%@日",publishTimeArray[0]];
    [cell.contentView addSubview:publishTimeLabel];

}

- (void)DirectorAndActorCell:(UITableViewCell*)cell
{
    NSDictionary *dataCell = self.movieDetail;
    UIImageView *BuyTicket = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 300, 35)];
    BuyTicket.layer.cornerRadius = 2.0;
    BuyTicket.image = [UIImage imageNamed:@"movie/movie_buy.png"];
    //人机交互
    BuyTicket.userInteractionEnabled = YES;
    UITapGestureRecognizer *BuyTicketGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapBuyTicket:)];
    [BuyTicket addGestureRecognizer:BuyTicketGesture];
    [cell addSubview:BuyTicket];
    
    UILabel *directorLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, 300, 20)];
    NSString *director = [Common valueForKey:P_DIRECTOR dic:dataCell];
    directorLabel.font = [UIFont fontWithName:@"TrebuchetMS" size:17.0];
    directorLabel.text = [NSString stringWithFormat:@"导演：%@",director];
    [cell.contentView addSubview:directorLabel];
    
    UILabel *actorLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 90, 300, 20)];
    NSString *actor = [Common valueForKey:P_MAIN_ACTOR dic:dataCell];
    actorLabel.font = [UIFont fontWithName:@"TrebuchetMS" size:17.0];
    actorLabel.text = [NSString stringWithFormat:@"主演：%@",actor];
    [cell.contentView addSubview:actorLabel];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(10, DIRECTORACTORCELL, 300, 1)];
    lineView.backgroundColor = [Common colorWithRGB:0xDFDFDF];
    [cell.contentView addSubview:lineView];
}

- (void)INTROCELL:(UITableViewCell*)cell
{
    NSDictionary *dataCell = self.movieDetail;
    NSString *movieIntro = [Common valueForKey:P_INTRO dic:dataCell];
    UILabel *intro = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 60, 20)];
    intro.text = @"剧情：";
    [cell.contentView addSubview:intro];
    UILabel *introLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, -10, 240, INTROCELL)];
    introLabel.font = [UIFont fontWithName:@"TrebuchetMS" size:17.0];
    introLabel.numberOfLines = 10;
    introLabel.text = movieIntro;
    [cell.contentView addSubview:introLabel];
}

#pragma mark - Table view data source
//返回table中有多少个组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 3;
}

//返回table中一个组有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

//用cellForRowAtIndexPath来得到一个包含每一行显示信息的UITableViewCell对象.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    switch (indexPath.section) {
        case 0:
        {
            
            cell = [tableView dequeueReusableCellWithIdentifier:@"SingleMovieDetailCell"];
            // Configure the cell...
            if(cell == nil)
            {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SingleMovieDetailCell"];
            }
            [self loadTitleCell:cell];
            break;
        }
        case 1:
        {
            cell = [tableView dequeueReusableCellWithIdentifier:@"DirectorActorCell"];
            // Configure the cell...
            if(cell == nil)
            {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DirectorActorCell"];
            }
            [self DirectorAndActorCell:cell];
            break;
        }
        case 2:
        {
            cell = [tableView dequeueReusableCellWithIdentifier:@"MOVIEINTROCELL"];
            // Configure the cell...
            if(cell == nil)
            {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MOVIEINTROCELL"];
                
            }
            [self INTROCELL:cell];
            break;
        }
        default:
        {
            cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
            // Configure the cell...
            if(cell == nil)
            {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
            }
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

//每一个cell定义不同的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0) {
        return TITLECELL_HEIGHT;
    }
    else if (indexPath.section == 1) {
        return DIRECTORACTORCELL;
    }
    else if (indexPath.section == 2) {
        return INTROCELL;
    }

    else{
        return 100;
    }
}

#pragma mark - delegate

- (void)onTapBuyTicket:(UITapGestureRecognizer*)tap
{
    NSLog(@"onTapBuyTicket");
    ChooseCinemaViewController *chooseCinemaViewCtrl = [[ChooseCinemaViewController alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    chooseCinemaViewCtrl.movieId = [[Common valueForKey:P_ID dic:_movieDetail] integerValue];
    [self.navigationController pushViewController:chooseCinemaViewCtrl animated:YES];
}

#pragma mark - private
//截取图片部分
- (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect {
    CGImageRef sourceImageRef = [image CGImage];
    CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
    return newImage;
}


@end
